<html>
<head>
<title>计算器</title>
</head>
<meta charset="utf-8"/>


<?php
   $num1="true";
   $num2="true";

//print_r($_GET);
   if(isset($_GET["sub"])){
      //再判断值不能为空,""表示为空
   	     if($_GET["num1"]==""){
               $num1="false";
               $message="第一个数不能为空";
   	     }
         if($_GET["num2"]==""){
               $num2="false";
               $message="第二个数不能为空";
         }

         if($num1 && $num2){   	 
   	           $sum=0;
               switch($_GET["ysf"]){
       	       case "+":
       	               $sum=$_GET["num1"]+$_GET["num2"];
       	               break;
       	       case "-":
       	               $sum=$_GET["num1"]-$_GET["num2"];
       	               break;
       	       case "x":
       	               $sum=$_GET["num1"]*$_GET["num2"];
       	               break;
       	       case "/":
       	               $sum=$_GET["num1"]/$_GET["num2"];
       	               break;
       	       case "%":
       	               $sum=$_GET["num1"]%$_GET["num2"];
       	               break;
                }
           }
       }
  ?>


<body>
     <table align="center" width="500" border="1">
     	<caption><h1>php实现简单计算器</h1></caption>
         <form action="2016.10.19.jsq.php">
         	<tr>
         		<td>
         			<input type="text" size="5" name="num1">
         		</td>
         		<td>
                   <select name="ysf">
                   	  <option value="+">+</opiton>
                      <option value="-">-</opiton>
                      <option value="x">x</opiton>
                      <option value="/">/</opiton>
                      <option value="%">%</opiton>   	  	
                   </select>    
         		</td>
         		<td>
         			<input type="text" size="5" name="num2">
         		</td>
         		<td>
         			<input type="submit" name="sub" value="计算">
         		</td>	
            </tr>
            
            <?php
            //显示计算过程和结果
            	if(isset($_GET["sub"])){
                	echo '<tr><td colspan="5">';
                	if($num1 && $num2){
                	echo "结果：".$_GET["num1"]." ".$_GET["ysf"]." ".$_GET["num2"]." = ".$sum;
                	 }else{
                	 	echo $message;
                	 }
                	echo '</td></tr>';
                }
              
            ?>
         </form>
     </table>
</body>
</html>